/*
 * @Descripttion: |
 * @version: 1.0
 * @Author: 王娆
 * @Date: 2020-08-23 17:35:04
 * @LastEditors: 王娆
 * @LastEditTime: 2020-08-27 19:42:46
 */
import { loginuser,getUserInfo } from '../../api/login'
import Cookies from 'js-cookie'
import router from '../../router'
import {Message} from 'element-ui'
const state = {

}

const mutations = {
    
}

const actions = {
    async loginUser ({dispatch},payload) {
        const { data: res } = await loginuser(payload)
        console.log(res.token)
        if(res.code !== 1){
            return Message.error(res.msg)
        }
        Message.success(res.msg)
        dispatch('getUserIdentity')
        Cookies.set('token',res.token)
        router.push('/home/welcome')
    },
    async getUserIdentity ({commit,dispatch}){
        const {data: res} = await getUserInfo()
        console.log(res)
        if(res.code !==1) {
           return Message.error(res.msg)
        }
        dispatch('GetAuth/getRoutes',res.data.identity_text,{root:true})
        sessionStorage.setItem('user_id',res.data.user_id)
        sessionStorage.setItem('identity_text',res.data.identity_text)
    },
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}

